﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.Entity;

public partial class masterdata_Distribution_Maintain : PageBase
{
    private decimal _customerID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["customerID"] != null)
        {
            decimal.TryParse(Request["customerID"], out _customerID);
        }

        if (!IsPostBack)
        {
            //分配类型
            ddlAssigned.Items.Add(new ListItem("--所有--", "0"));
            ddlAssigned.Items.Add(new ListItem("已分配", "1"));
            ddlAssigned.Items.Add(new ListItem("未分配", "2"));
            ddlAssigned.SelectedValue = "1";

            BindGrid();
            ddlAssigned.SelectedValue = "0";
        }
        btnSubmiet.Attributes["onClick"] = "Submit(this)";
    }

    private void BindGrid()
    {
        if (_customerID > 0)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder("");

            sb.Append(string.Format(" AND CHAINS_ORG.ORG_ID IN (SELECT ID FROM [FC_GetOrgWithParentID]('{0}','{1}'))", MyStateManager.DomainID, MyStateManager.OrgID));

            if (txtCode.Text.Trim() != string.Empty)
            {
                sb.Append(" AND chains.code LIKE '%" + txtCode.Text.Trim() + "%'");
            }
            if (txtName.Text.Trim() != string.Empty)
            {
                sb.Append(" AND chains.description LIKE '%" + txtName.Text.Trim() + "%'");
            }

            DataTable dt = ChainManager.SearchChainsForCustomer(_customerID, ddlAssigned.SelectedValue, sb.ToString());
            gvDistribution.DataSource = dt;
            gvDistribution.DataBind();
        }
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void gvDistribution_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDistribution.PageIndex = e.NewPageIndex;
        BindGrid();
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Customer_Maintain.aspx?id=" + _customerID.ToString());
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        decimal chainsID = 0;
        decimal CCID = 0;
        decimal supplyID;
        CUSTOMER_CHAINS customerChains;
        CheckBox ckx = new CheckBox();
        Control ctl;
        DropDownList ddlSupply;

        if (_customerID > 0)
        {
            //先检查是否有取消绑定
            for (int i = 0; i < gvDistribution.Rows.Count; i++)
            {
                ctl = gvDistribution.Rows[i].FindControl("cbxSelected");
                if (ctl != null)
                    ckx = ctl as CheckBox;
                decimal.TryParse(gvDistribution.DataKeys[i].Values[1].ToString(), out CCID);
                decimal.TryParse(gvDistribution.DataKeys[i].Values[0].ToString(), out chainsID);

                //Steven jiang
                //20120-10-23 
                //if (!ckx.Checked && CCID != 0)
                //{
                //    DateTime deleteDate = DateTime.Parse(ConfigManager.GetItemList(ConfigEnum.Delete_Time, MyStateManager.DomainID).Value);
                //    if (DateTime.Now.Hour < deleteDate.Hour)
                //    {
                //        ShowAndRedirect(this,"无法取消挂接，请在" + deleteDate.ToShortTimeString() + "后再操作!", Request.RawUrl);
                //        return;
                //    }
                //}

            }

            for (int i = 0; i < gvDistribution.Rows.Count; i++)
            {
                ctl = gvDistribution.Rows[i].FindControl("cbxSelected");
                ddlSupply = (DropDownList)gvDistribution.Rows[i].FindControl("ddlSupply");

                if (ctl != null)
                {
                    ckx = ctl as CheckBox;
                }
                decimal.TryParse(gvDistribution.DataKeys[i].Values[0].ToString(), out chainsID);
                decimal.TryParse(gvDistribution.DataKeys[i].Values[1].ToString(), out CCID);

                if (ckx != null && chainsID > 0)
                {
                    customerChains = new CUSTOMER_CHAINS();
                    customerChains.CHAINSID = int.Parse(chainsID.ToString());
                    customerChains.CUSTOMERID = _customerID;
                    customerChains.DOMAINID = this.MyStateManager.DomainID;
                    customerChains.LAST_UPDATER = this.MyStateManager.Name;
                    customerChains.LAST_UPDATE_TIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeDecimal(DateTime.Now);
                    decimal.TryParse(ddlSupply.SelectedValue, out supplyID);
                    customerChains.SUPPLYID = supplyID;

                    ChainManager.AssignChainForCustomer(ckx.Checked, CCID != 0, CCID, customerChains, MyStateManager.DomainID);
                }
            }
        }
        Response.Redirect("Customer_Maintain.aspx?id=" + _customerID.ToString());
    }

    protected void gvDistribution_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            decimal chainID;
            decimal.TryParse(gvDistribution.DataKeys[e.Row.RowIndex].Values["id"].ToString(), out chainID);

            //绑定该经销商下所有的供应商
            DropDownList ddlSupply = (DropDownList)e.Row.FindControl("ddlSupply");
            ddlSupply.DataSource = new SupplyManager().SelecctSupplyByChain(chainID, 1);
            ddlSupply.DataTextField = "NAME";
            ddlSupply.DataValueField = "ID";
            ddlSupply.DataBind();
            ListItem item = new ListItem("--未分配--", "");
            item.Attributes["style"] = "color:Red";
            ddlSupply.Items.Insert(0, item);

            try
            {
                CUSTOMER_CHAINS custChains = new ChainManager().LoadCustomerChain(_customerID, chainID);
                if (custChains != null)
                    ddlSupply.SelectedValue = custChains.SUPPLYID.ToString();
            }
            catch { }
        }
    }
}
